package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 51遥控输出
 * Created by zwz on 2020/4/22.
 */
public class Cmd_51Fragment extends BaseFragment {

    @BindView(R.id.sp_id)
    Spinner spId;
    @BindView(R.id.rbtn_open)
    RadioButton rbtnOpen;
    @BindView(R.id.rbtn_close)
    RadioButton rbtnClose;
    @BindView(R.id.edt_input)
    EditText edtInput;


    public static Cmd_51Fragment newInstance() {
        Bundle args = new Bundle();
        Cmd_51Fragment fragment = new Cmd_51Fragment();
        fragment.setArguments(args);


        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_51;
    }

    @Override
    protected void initView() {

        String[] ids = {"1", "2", "3", "4"};
        ArrayAdapter<String> mAdapter = new ArrayAdapter<>(_mActivity, android.R.layout.simple_spinner_dropdown_item, ids);
        spId.setAdapter(mAdapter);

    }



    private void set() {
        String id = String.format(Locale.CHINA, "%02d", Integer.parseInt(spId.getSelectedItem().toString()));
        //启动为 0xCC， 停止为 0x33
        String state;
        String time;
        if (rbtnOpen.isChecked()){
            if (  edtInput.getText().toString().isEmpty()) {
                toastShow("请填写时间");
                return;
            }
            state="CC";
            time=String.format(Locale.CHINA, "%04x",Integer.parseInt(edtInput.getText().toString()));
        }else {
            state="33";
            time="0000";
        }


        sendData(id + " " + state + " " + time.substring(2,4)+" "+time.substring(0,2));
    }

    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_51, data);

        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();

                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_51)) {
                    if (data[15].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

                }


            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }
    @OnClick(R.id.btn_set)
    public void onViewClicked() {
        set();
    }

}
